package com.umfb.bootstrap.intercepter;

import org.apache.log4j.Logger;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by gzheng on 17-11-17.
 */
public class ParamsValidateInterceptor extends AbstractInterceptor {
    /** 加载分页最大值*/
    private Integer maxPageNum = 100;
    /** 每页加载数据条数最大值*/
    private Integer maxItemPerPage = 100;

    private Logger log = Logger.getLogger(this.getClass());

    public Integer getMaxPageNum() {
        return maxPageNum;
    }

    public void setMaxPageNum(Integer maxPageNum) {
        this.maxPageNum = maxPageNum;
    }

    public Integer getMaxItemPerPage() {
        return maxItemPerPage;
    }

    public void setMaxItemPerPage(Integer maxItemPerPage) {
        this.maxItemPerPage = maxItemPerPage;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(handler instanceof HandlerMethod){
            String param = null;
//            boolean validate = true;
            HandlerMethod method = (HandlerMethod)handler;
            MethodParameter[] mps = method.getMethodParameters();

            for (MethodParameter mp : mps) {
                RequestParam anno = mp.getParameterAnnotation(RequestParam.class);
                if(anno == null)//该参数没有添加RequestParam注解，默认不进行参数值验证。
                    continue;
                param = anno.value();
                String value = request.getParameter(param);
                log.info("参数"+param+"值:"+value);
            }


        }
        return super.preHandle(request, response, handler);
    }
}
